package com.graduate.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduate.common.entity.Handle;
import com.graduate.common.entity.House;
import com.graduate.common.entity.Reserve;
import com.graduate.common.mapper.HandleMapper;
import com.graduate.common.mapper.HouseMapper;
import com.graduate.common.service.IHandleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduate.common.utils.PageUtils;
import com.graduate.common.vo.HandleVo;
import com.graduate.common.vo.ReserveVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zfy
 * @since 2021-05-09
 */
@SuppressWarnings("ALL")
@Service
public class HandleServiceImpl extends ServiceImpl<HandleMapper, Handle> implements IHandleService {


    @Autowired
    private HouseMapper houseMapper;

    @Override
    public IPage<HandleVo> selectHandlePage(Map<String, Object> map) {
        // 设置分页查询对象
        IPage<Handle> page = (IPage<Handle>) PageUtils.convertToPage(map);
        QueryWrapper<Handle> wrapper = new QueryWrapper<>();
        // page为查询出的分页对象
        page = baseMapper.selectPage(page, wrapper);
        // 取出page中的数据转换成VO对象
        List<Handle> records = page.getRecords();
        List<HandleVo> houseVos = records.stream().map(record -> {
            HandleVo handleVo = new HandleVo();
            // 把数据库中的预定信息全部封装进入vo对象中
            BeanUtils.copyProperties(record, handleVo);
            // 从数据库中查询出house信息封装进入vo对象中
            House house = houseMapper.selectById(record.getHid());
            handleVo.setHouse(house);
            return handleVo;
        }).collect(Collectors.toList());
        Page<HandleVo> ans = new Page<>();
        BeanUtils.copyProperties(page, ans);
        ans.setRecords(houseVos);
        return ans;
    }
}
